<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\MeritBox as MeritBoxModel;
use \think\Db;
use \think\Controller;
/**
 * 申领功德箱控制器
 * Class Activeapply
 * @package app\home\controller
 */
class Meritbox extends Controller
{
    /**
     * [meritbox  申领功德箱报名]
     * @param
     * @return json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $model = new MeritBoxModel;

        $merit = $model->findData($param['phone'],'phone');
        if(!empty($merit)){
            return admin_json(5,'该手机号已经申领！');
        }
        //$mem = $this->user;
        $map = [];
        $map['tel'] = $param['phone'];
        $map['code'] = $param['code'];

        $result = Db::name('vercode')->where($map)->find();
        if(!empty($result)) {
            if ($result['status'] == 2) {
                return admin_json(4, '验证码已使用');
            }
            if (time() - $result['addtime'] > config('home.code_expiration_time')) {
                return admin_json(3, '验证码已过期');
            }
            Db::name('vercode')->where($map)->update(['status' => 2]);
        }else{
            return admin_json(2,'手机号或验证码错误');
        }

        try{
            //$param['mid'] = $mem['id'];
            $model->addData($param);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '报名失败'.$e->getMessage());
        }
        return admin_json(1, '报名成功');
    }
    /**
     * [count  申领功德箱报名人数]
     * @param
     * @return json
     * @author [C。]
     */
    public function count()
    {
        $model = new MeritBoxModel;
        $allcount = $model->getAllCount([]);
        return admin_json(1, '获取成功',153+$allcount);
    }
}